package com.ruoyi.system.service.impl;

import java.util.Date;
import java.util.List;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.enums.TalentInfoImportEnum;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.system.domain.TalNoticePayment;
import com.ruoyi.system.domain.TalTalentInfo;
import com.ruoyi.system.service.ITalNoticePaymentService;
import com.ruoyi.system.service.ITalTalentCirculateService;
import com.ruoyi.system.service.ITalTalentInfoService;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.TalCuptureNoticeMapper;
import com.ruoyi.system.domain.TalCuptureNotice;
import com.ruoyi.system.service.ITalCuptureNoticeService;

/**
 * 党费收缴通知Service业务层处理
 *
 * @author anxinm
 * @date 2024-04-07
 */
@Service
public class TalCuptureNoticeServiceImpl extends ServiceImpl<TalCuptureNoticeMapper ,TalCuptureNotice> implements ITalCuptureNoticeService
{
    @Autowired
    private TalCuptureNoticeMapper talCuptureNoticeMapper;

    @Autowired
    private ITalTalentInfoService infoService ;

    @Autowired
    private ITalNoticePaymentService paymentService ;

    /**
     * 查询党费收缴通知
     *
     * @param id 党费收缴通知主键
     * @return 党费收缴通知
     */
    @Override
    public TalCuptureNotice selectTalCuptureNoticeById(Long id)
    {
        return talCuptureNoticeMapper.selectTalCuptureNoticeById(id);
    }

    /**
     * 查询党费收缴通知列表
     *
     * @param talCuptureNotice 党费收缴通知
     * @return 党费收缴通知
     */
    @Override
    public List<TalCuptureNotice> selectTalCuptureNoticeList(TalCuptureNotice talCuptureNotice)
    {
        return talCuptureNoticeMapper.selectTalCuptureNoticeList(talCuptureNotice);
    }

    /**
     * 新增党费收缴通知
     *
     * @param talCuptureNotice 党费收缴通知
     * @return 结果
     */
    @Override
    public int insertTalCuptureNotice(TalCuptureNotice talCuptureNotice)
    {

        talCuptureNotice.setCreateTime(DateUtils.getNowDate());
        int insert = talCuptureNoticeMapper.insert(talCuptureNotice);

        /**党员催缴记录*/
        LambdaQueryWrapper<TalTalentInfo> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TalTalentInfo::getImportState , TalentInfoImportEnum.TALENT_IMPORT.getCode()) ;
        List<TalTalentInfo> list = infoService.list(wrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            for (TalTalentInfo info : list) {
                TalNoticePayment payment = new TalNoticePayment();
                payment.setNoticeId(talCuptureNotice.getId());
                payment.setNoticeName(talCuptureNotice.getTitle());
                payment.setUserId(info.getUserId());
                payment.setUserName(info.getName());
                payment.setCreateBy(SecurityUtils.getLoginUser().getUser().getNickName());
                payment.setCreateTime(new Date());
                payment.setRemark(talCuptureNotice.getRemark());
                paymentService.save(payment) ;
            }
        }
        return insert;
    }

    /**
     * 修改党费收缴通知
     *
     * @param talCuptureNotice 党费收缴通知
     * @return 结果
     */
    @Override
    public int updateTalCuptureNotice(TalCuptureNotice talCuptureNotice)
    {
        talCuptureNotice.setUpdateTime(DateUtils.getNowDate());
        return talCuptureNoticeMapper.updateById(talCuptureNotice);
    }

    /**
     * 批量删除党费收缴通知
     *
     * @param ids 需要删除的党费收缴通知主键
     * @return 结果
     */
    @Override
    public int deleteTalCuptureNoticeByIds(Long[] ids)
    {
        return talCuptureNoticeMapper.deleteTalCuptureNoticeByIds(ids);
    }

    /**
     * 删除党费收缴通知信息
     *
     * @param id 党费收缴通知主键
     * @return 结果
     */
    @Override
    public int deleteTalCuptureNoticeById(Long id)
    {
        return talCuptureNoticeMapper.deleteTalCuptureNoticeById(id);
    }
}
